package net;

import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

/**
 * 
 * @author Noah Picard
 * Practice Code:
 * Testing writing to a PDF file.
 * PDF Document W = 612, H = 792 
 */

public class PDFEditor {

	public static void main(String[] args) throws IOException, COSVisitorException {
		System.out.println("Starting");
		
		PDDocument document = new PDDocument();
		
		PDPage page = new PDPage();
		document.addPage(page);
		
		PDFont font = PDType1Font.HELVETICA_BOLD;
		PDPageContentStream contentStream = new PDPageContentStream(document, page);
		
		contentStream.beginText();
		contentStream.setFont(font, 12);
		contentStream.moveTextPositionByAmount(100, 700);
		contentStream.drawString("Hello Mankey \n\r How is your sock today?");
		contentStream.endText();
		
		float[] rectX = {1,1,611,611};
		float[] rectY = {1,611,611,1};
		contentStream.drawPolygon(rectX, rectY);
		contentStream.close();
		
		
		document.save("src/resources/editing.pdf");
		
		document.close();
		
		/*try {
			//Opens PDF as an read/write document
			PDDocument pdd = PDDocument.load(new File("src/resources/testing.pdf"));
			
			//Opens PDFTextStripper to get text from p
			PDFTextStripper p = new PDFTextStripper();
			String text = p.getText(pdd);
			pdd.close();
			
			//Opens text document to write PDF text into
			FileWriter fw = new FileWriter("src/resources/testAsTxt.txt");
			fw.write(text);
			fw.close();
			
			System.out.println(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		System.out.println("Finished");
	}

}
